package cn.itcast.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;

import cn.itcast.pojo.Permission;
import cn.itcast.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.QueryPageBean;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@CrossOrigin
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @GetMapping
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = this.permissionService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }

    @PostMapping
    public Result add(@RequestBody Permission permission) {
        try {
            this.permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }

    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        try {
            this.permissionService.delete(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }

    }

    @PutMapping
    public Result update(@RequestBody Permission permission) {

        try {
            this.permissionService.update(permission);
            return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }

    }

    //数据回显
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {

        try {
            Permission byId = this.permissionService.findById(id);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 根据菜单id查询权限信息
     *
     * @param menuIds
     * @return
     */
    @GetMapping("/menu/association")
    public Result findByMenuIdsWithPermission(@RequestParam Integer[] menuIds) {
        List<Permission> permissionList = this.permissionService.findByMenuIdsWithPermission(menuIds);
        if (CollectionUtil.isEmpty(permissionList)) {
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL, permissionList);
        }
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("all")
    public Result findAll() {
        try {
            List<Permission> permissionList = this.permissionService.findAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
}
